/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iss.examsys.ejb;

import com.iss.examsys.entity.LecturerModule;
import com.iss.examsys.entity.Role;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author LecturerModule
 */
@Stateless
public class LecturerModuleBean {

    @PersistenceContext EntityManager em;

    public void create(LecturerModule lecturerModule){
        
        em.persist(lecturerModule);
    }
    
    public void edit(LecturerModule lecturerModule) {
        
        em.merge(lecturerModule);
    }
    
    public void remove(LecturerModule lecturerModule) {
        em.remove(em.merge(lecturerModule));
    }
    
    public LecturerModule findById(long id){        
        LecturerModule entity = em.find(LecturerModule.class, id);        
        return entity;
    }
           
    public List<LecturerModule> findLecturerModulesByLecturerId(String userId){
        
        List resultList = em.createNativeQuery("select * from lecturer_module lm, module m where m.ID = lm.MODULE_ID and lm.LECTURER_ID = ?")
                .setParameter(1, userId).getResultList();
        
        return resultList;
    }
    
    public LecturerModule findByLecturerModuleId(String lecturerModuleId){        
        TypedQuery<LecturerModule> result = em.createNamedQuery("LecturerModule.findByLecturerModuleId", LecturerModule.class);
        result.setParameter("lecturerModuleId", lecturerModuleId);
        
        LecturerModule lecturerModule = null;
        
        if(result.getResultList().size()>0){
            lecturerModule = result.getResultList().get(0);
        }
    
        return lecturerModule;
    }
    
    public LecturerModule findByLecturerModuleEmail(String lecturerModuleEmail){        
        TypedQuery<LecturerModule> result = em.createNamedQuery("LecturerModule.findByEmail", LecturerModule.class);
        result.setParameter("email", lecturerModuleEmail);
        
        LecturerModule lecturerModule = null;
        
        if(result.getResultList().size()>0){
            lecturerModule = result.getResultList().get(0);
        }
    
        return lecturerModule;
    }
    
    private static String sha256(String base) {
            try{
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] hash = digest.digest(base.getBytes("UTF-8"));
                StringBuilder hexString = new StringBuilder();

                for (int i = 0; i < hash.length; i++) {
                    String hex = Integer.toHexString(0xff & hash[i]);
                    if(hex.length() == 1) hexString.append('0');
                    hexString.append(hex);
                }

                return hexString.toString();
            } catch(NoSuchAlgorithmException | UnsupportedEncodingException ex){
                throw new RuntimeException(ex);
            }
    }
}
